﻿
<div id="tg-menu" data-options="fit:true,striped:true,toolbar:'#toolbar'">
</div>
<div id="toolbar" style="padding: 5px; height: auto">
    <div>
        @{
            if (ViewBag.Create)
            {
            <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true"
                onclick="Create()">新建</a> 
            }
            if (ViewBag.Edit)
            {
            <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true"
                onclick="Edit()">编辑</a> 
            }
            if (ViewBag.Delete)
            { 
            <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true"
                onclick="Remove()">删除</a>
            }
            <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="Reload()">刷新</a>
        }
    </div>
</div>

<script>
    $(function () {
        var checkflag = false;
        var checkallflag = false;
       

        $("#tg-menu").treegrid({
            //title: "菜单列表",
            url: "/Menu/MenuJson",
            idField: "ID",
            treeField: 'Name',
            rownumbers: true,
            border:false,
            singleSelect: false,
            checkOnSelect : false,
            selectOnCheck: false,
            deepCascade:true,
            columns: [[
                    {
                        field: 'ID', checkbox: true,
                        styler: function (value, row, index) {
                            if (row.HasChild) {
                                return 'background-color:#E0E0E0;';
                            }
                        }
                    },
                    { field: 'Name', title: '名称', width: 200, align: 'left' },
                    { field: 'SortNumber', title: '排序号', width: 100, align: 'center' },
                    { field: 'iconCls', title: '图标样式', width: 100, align: 'center' },
                    { field: 'MenuUrl', title: '链接地址', width: 200, align: 'center' },
                    {
                        field: 'IsShow', title: '是否显示', width: 80, align: 'center',
                        formatter: function (value) {
                            if (value) {
                                return "是";
                            }
                            else {
                                return "否";
                            }
                        }
                    },
                    
                    { field: 'CreateRealName', title: '创建人', width: 120, align: 'center' },
                    { field: 'CreateDate', title: '创建时间', width: 120, align: 'center' },
                    { field: 'LastModifyRealName', title: '最后修改人', width: 120, align: 'center' },
                    { field: 'LastModifyDate', title: '最后修改时间', width: 120, align: 'center' },
            ]],
            onLoadSuccess: function (row, data) {

            },
            onDblClickRow: function (rowData) {
                if ("@ViewBag.Edit".toLowerCase()=="true") {
                    Edit(rowData);
                }
            },
            //onCheck: function (rowData) {
            //    alert(1);
            //},
            //onUncheck: function (rowData){
            //    alert(2);
            //},
            onCheck: function (rowData) {
                //若HasChild为true，则给出提示
                $.fn.treegrid.defaults.onCheck.apply(this, arguments);
                if (rowData.HasChild) {
                    if (!checkflag) {
                        $(this).treegrid('uncheckRow', rowData.ID);

                        $.messager.confirm('确认', '所选菜单已存在子菜单，若删除它，将同时删除它的子菜单以及功能权限，确认选中吗?', function (r) {
                            if (r) {
                                checkflag = true;
                                $("#tg-menu").treegrid('checkRow', rowData.ID);
                            }
                            else {
                                $("#tg-menu").treegrid('uncheckRow', rowData.ID);
                            }
                        });
                    }
                }
            },
            onUncheck: function (rowData) {
                $.fn.treegrid.defaults.onUncheck.apply(this, arguments);
                checkflag = false;
            },
            onCheckAll: function (rows) {
                if (!checkallflag) {
                    $(this).treegrid('uncheckAll');
                    $.messager.confirm('确认', '所选菜单可能存在子菜单，若删除它们，将同时删除它们的子菜单以及功能权限，确认全部选中吗?', function (r) {
                        if (r) {
                            checkflag = false;
                            checkallflag = true;
                            $("#tg-menu").treegrid('checkAll');
                        }
                        else {
                            checkflag = false;
                            //若HasChild为true，则不让复选框选中
                            for (var i = 0; i < rows.length; i++) {
                                if (rows[i].HasChild) {
                                    $("#tg-menu").treegrid('uncheckRow', rows[i].ID);
                                }
                                else {
                                    $("#tg-menu").treegrid('checkRow', rows[i].ID);
                                }
                            }

                        }
                    });
                }
            },
            onUncheckAll: function (rows) {
                checkallflag = false;
            }
        });
    });

    function Create()
    {
        var node = $("#tg-menu").treegrid("getSelected");
        if (node) {
            
            OpenNewTab("新建菜单", "/Menu/CreateEdit?type=Create&menuid=" + node.ID);
        }
        else {
            
            OpenNewTab("新建菜单", "/Menu/CreateEdit");
        }
    }

    function Edit(node) {
        if (node == null)
        {
            node = $("#tg-menu").treegrid("getSelected");
        }
        if (node) {
          
            OpenNewTab("编辑菜单", "/Menu/CreateEdit?type=Edit&menuid=" + node.ID);
        }
    }

    function Reload()
    {
        $('#tg-menu').treegrid('clearSelections');
        $('#tg-menu').treegrid('clearChecked');
        $("#tg-menu").treegrid("reload");
    }

    function Save(type) {
        if ($('#' + type + 'fm').form('validate'))
        {
           
            //alert($("#" + type + "fm").serialize() + "要添加的：" + addfunctionids + "要去掉的：" + delfunctionids); return;
            //alert(JSON.stringify($("#" + type + "fm").serializeObject())); return;
            $.post("/Menu/Save", { menumodel: JSON.stringify($("#" + type + "fm").serializeObject())}, function (result) {
                if (result.success) {
                    $.messager.alert('提示', '数据已保存!');
                    if (type == "Create") {
                        $('#tabs-menu').tabs('close', "新建菜单");
                    }
                    else {
                        $('#tabs-menu').tabs('close', "编辑菜单");
                    }
                    $('#tabs-menu').tabs('select', "菜单列表");
                    Reload();
                } else {
                    $.messager.alert('提示', result.msg, 'error');
                }
            }, 'json');
        }
    }

    function Remove() {
        var rows = $('#tg-menu').treegrid('getChecked');
        if (rows.length != 0) {
            var data = [];
            $.messager.confirm('确认', '您确定要删除所选项吗?', function (r) {
                if (r) {
                    //判断选中的项是否 可以被删除
                    for (var i = 0; i < rows.length; i++) {
                        data.push(rows[i].ID);
                    }
                    if (data.length == 0) {
                        return;
                    }
                    else {
                        $.post("/Menu/Remove", { ids: data.join(',') }, function (result) {
                            if (result.success) {
                                Reload();
                            } else {
                                $.messager.alert('提示', result.msg, 'error');
                            }
                        }, "json");
                    }
                }
            });
        }
    }

  
    function isDel(element, index, array) {
        if (element == this) {
            delete array[index];
            return false;
        }
        return true;
    }
</script>